package com.lkxicy.wos.controller;


import com.lkxicy.wos.pojo.entity.Staff;
import com.lkxicy.wos.service.StaffService;
import com.lkxicy.wos.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lkxicy
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/wos/v1/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 查找全部员工信息（分页）
     * @return
     */
    @GetMapping("searchPageStaff")
    public Response searchPageStaff(@RequestParam(value = "pageNum",defaultValue ="1") int pageNum,
                                   @RequestParam(value = "limit",defaultValue = "5") int limit){
        return Response.success(staffService.searchPageStaff(pageNum,limit));
    }

    /**
     * 查找全部员工信息
     * @return
     */
    @GetMapping("searchAllStaff")
    public Response searchAllStaff(){
        return Response.success(staffService.searchAllStaff());
    }

    /**
     * 查询某个工号的员工的信息
     * @return
     */
    @GetMapping("searchOneStaff")
    public Response searchOneStaff(@RequestParam("staffNumber") String staffNumber){
        return Response.success(staffService.searchOneStaff(staffNumber));
    }

    /**
     * 修改员工信息
     * @return
     */
    @PutMapping("changeStaff")
    public Response changeStaff(@RequestParam("staffNumber") String staffNumber,
                                @RequestParam("name") String name,
                                @RequestParam("department") String department,
                                @RequestParam("phoneNumber") String phoneNumber,
                                @RequestParam("sex") String sex,
                                @RequestParam("email") String email,
                                @RequestParam("salary") Double salary,
                                @RequestParam("id") int id){
        Staff staff = new Staff();
        staff.setId(id);
        staff.setStaffNumber(staffNumber);
        staff.setName(name);
        staff.setDepartment(department);
        staff.setPhoneNumber(phoneNumber);
        staff.setSex(sex);
        staff.setEmail(email);
        staff.setSalary(salary);
        return Response.success(staffService.changeStaff(staff));
    }

    /**
     * 删除员工信息(逻辑删除)
     * @return
     */
    @DeleteMapping("deleteStaff")
    public Response deleteStaff(@RequestParam("staffNumber") String staffNumber){
        return Response.success(staffService.deleteStaff(staffNumber));
    }

    /**
     * 管理员重置密码
     * @param staffNumber
     * @return
     */
    @PutMapping("resetPassword")
    public Response resetPassword(@RequestParam("staffNumber") String staffNumber){
        return Response.success("重置密码成功！",staffService.resetPassword(staffNumber));
    }

    /**
     * 新增员工
     * @return
     */
    @PostMapping("addStaff")
    public Response addStaff(@RequestParam("staffNumber") String staffNumber,
                             @RequestParam("name") String name,
                             @RequestParam("password") String password,
                             @RequestParam("department") String department,
                             @RequestParam("phoneNumber") String phoneNumber,
                             @RequestParam("sex") String sex,
                             @RequestParam("email") String email,
                             @RequestParam("salary") double salary){

        Staff staff = new Staff();
        staff.setStaffNumber(staffNumber);
        staff.setName(name);
        staff.setPassword(password);
        staff.setDepartment(department);
        staff.setPhoneNumber(phoneNumber);
        staff.setSex(sex);
        staff.setEmail(email);
        staff.setSalary(salary);
        return Response.success(staffService.addStaff(staff));
    }

    /**
     * 员工改密码
     * @param staffNumber
     * @param password
     * @param newPassword
     * @return
     */
    @PutMapping("changeStaffPassword")
    public Response changeStaffPassword(@RequestParam("staffNumber") String staffNumber,
                                        @RequestParam("password") String password,
                                        @RequestParam("newPassword") String newPassword){
        return Response.success(staffService.changeStaffPassword(staffNumber,password,newPassword));
    }


    /**
     * 返回员工表中的所有出现过的部门
     * @return
     */
    @GetMapping("selectAllDepartment")
    public Response selectAllDepartment(){
        return Response.success(staffService.selectAllDepartment());
    }




}

